package com.aj.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.aj.utils.StringUtils;

public class EditUserInfoActivity extends BaseActivity {

	public static final int NICKNAME_INT = 0x990001;
	public static final int NAME_INT = 0x990002;
	public static final int SIGN_INT = 0x990003;
	public static final String EDITTYPE_STRING = "";
	public static final String RESULT_STRING = "RESULT_STRING";
	private EditText info;
	private Button sure_button;

	private Integer editType = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_userinfo);
		initView();
	}

	private void initView() {
		editType = getIntent().getIntExtra(EDITTYPE_STRING, -1);
		info = (EditText) findViewById(R.id.info);
		sure_button = (Button) findViewById(R.id.sure_button);
		ButtonClickEvent buttonClickEvent = new ButtonClickEvent();
		sure_button.setOnClickListener(buttonClickEvent);
		switch (editType) {
		case NICKNAME_INT:
			setActionBarTitle(R.string.edit_nickname_text);
			info.setHint((getString(R.string.edit_please_text) + getString(R.string.edit_nickname_text)));
			break;
		case NAME_INT:
			setActionBarTitle(getString(R.string.edit_name_text));
			info.setHint((getString(R.string.edit_please_text) + getString(R.string.edit_name_text)));
			break;
		case SIGN_INT:
			setActionBarTitle(getString(R.string.edit_sign_text));
			info.setHint((getString(R.string.edit_please_text) + getString(R.string.edit_sign_text)));
			break;
		default:
			setActionBarTitle(R.string.edit_title_text);
			break;
		}
	}

	class ButtonClickEvent implements OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.sure_button:
				if (StringUtils.isStringEmpty(info.getText().toString().trim())) {
					Toast.makeText(EditUserInfoActivity.this,
							getToastMessage(editType), Toast.LENGTH_LONG)
							.show();
					return;
				}
				Intent intent = new Intent();
				intent.putExtra(RESULT_STRING, info.getText().toString());
				setResult(editType, intent);
				break;
			}
			finish();
		}
	}

	private String getToastMessage(int editType) {
		String s = "";
		switch (editType) {
		case NICKNAME_INT:
			s = (getString(R.string.edit_please_text) + getString(R.string.edit_nickname_text));
			break;
		case NAME_INT:
			s = (getString(R.string.edit_please_text) + getString(R.string.edit_name_text));
			break;
		case SIGN_INT:
			s = (getString(R.string.edit_please_text) + getString(R.string.edit_sign_text));
			break;
		default:
			break;
		}
		return s;
	}
}
